package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import model.Cursocomplementar;
import util.ConexaoBD;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;


public class DAOCursoComplementar extends ModelDAO<Cursocomplementar>{
	private DAOAluno daoAluno = new DAOAluno();
	
	public DAOCursoComplementar() {
        super(Cursocomplementar.class);
    }
	
	public void save(Cursocomplementar cc){
		super.begin();
		super.save(cc);
		super.commit();
	}
	
	public void update(Cursocomplementar cc){
		super.begin();
		super.update(cc);
		super.commit();
	}
	
	public Cursocomplementar search(int cod){
		super.begin();
		Cursocomplementar cc = super.search(cod);
		super.commit();
		return cc;
	}
	
	public List<Cursocomplementar> listar(){
		super.begin();
        List<Cursocomplementar> list = super.findAll();
        super.commit();
        return list;
    }
	
	public void delete(Cursocomplementar cc){
		super.begin();
		super.delete(cc.getCodCC());
		super.commit();
	}
	
	public List<Cursocomplementar> listar(Integer codAluno) throws SQLException{
		// conectar banco
		Connection con = ConexaoBD.getConnection();
		PreparedStatement stmt = (PreparedStatement) con.prepareStatement ("select * from cursocomplementar, aluno where aluno.codAluno="+codAluno+" and cursocomplementar.codAluno=aluno.codAluno;");
		ResultSet rs = stmt.executeQuery();
			
		List<Cursocomplementar> cursocompl = new ArrayList<Cursocomplementar>();
			
		while (rs.next()){
		Cursocomplementar cc = new Cursocomplementar();
		cc.setCursoCC(rs.getString("cursoCC"));
		cc.setInstituicaoCC(rs.getString("instituicaoCC"));
		cc.setSituacaoCC(rs.getString("situacaoCC"));
		Integer codAl = rs.getInt("codAluno");
		cc.setAluno(daoAluno.search(codAl));
		cc.setCodCC(rs.getInt("codCC"));
		cc.setAnoFimCC(rs.getInt("anoFimCC"));
		cc.setCidadeCC(rs.getString("cidadeCC"));
		cc.setUfCC(rs.getString("ufCC"));		
		
		//adicionando o objeto a lista
		cursocompl.add(cc);
		}
		
		//}
		return cursocompl;
    }
}
